package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.AddAcount;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.Register;
import com.ttai.ui.activity.SetMiyaoAgain;
import com.ttai.ui.activity.UaP_Protect;
import com.ttai.untils.Constant;

import retrofit2.Call;

public class SetMiYaoAgainPresenter extends BasePresenter{
    SetMiyaoAgain setMiyaoAgain;
    private String token;

    public SetMiYaoAgainPresenter(SetMiyaoAgain setMiyaoAgain) {
        this.setMiyaoAgain = setMiyaoAgain;
    }

    @Override
    protected void showError(String message) {
        Toast.makeText(setMiyaoAgain,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseJson(JsonObject json) {
            token = Constant.getToken();
            encrypt(token,AddAcount.acountName,setMiyaoAgain.jiamiAcountId,setMiyaoAgain.jiamiAcountPassword);
//            dealDB();
        /**
         * 判断value是否存在,不存在则向acounttip表插入数据
         */
        if(setMiyaoAgain.value==null || setMiyaoAgain.value.isEmpty()){
            setKetTip(token,setMiyaoAgain.acountTip);
        }

        Toast.makeText(setMiyaoAgain,"加密成功",Toast.LENGTH_SHORT).show();
        setMiyaoAgain.finish();
        Intent intent1=new Intent();
        intent1.setClass(setMiyaoAgain,UaP_Protect.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
        setMiyaoAgain.startActivity(intent1);
    }

    /**
     * 进行newacount插入，判断value是否存在，不存在则向acounttip插入数据
     */
//    private void dealDB() {
//        DBHelper dbHelper = DBHelper.getInstance(setMiyaoAgain);
//        SQLiteDatabase db=dbHelper.getWritableDatabase();
//        ContentValues values=new ContentValues();
//        values.put("phone",setMiyaoAgain.phonenumber);
//        values.put("acountname",AddAcount.acountName);
//        values.put("acounttip",setMiyaoAgain.acountTip);
//        db.insert("newacount",null,values);
//        values.clear();
//
//        /**
//         * 判断value是否存在,不存在则向acounttip表插入数据
//         */
//        if(setMiyaoAgain.value==null || setMiyaoAgain.value.isEmpty()){
//            setKetTip(token,setMiyaoAgain.acountTip);
//        }
//
//        Toast.makeText(setMiyaoAgain,"加密成功",Toast.LENGTH_SHORT).show();
//        setMiyaoAgain.finish();
//        Intent intent1=new Intent();
//        intent1.setClass(setMiyaoAgain,UaP_Protect.class);
//        intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
//        setMiyaoAgain.startActivity(intent1);
//    }
//
//    public String checkMiYaoTip() {
//        String value = null;
//        String phonenumber;
//        if (HomePresrenter.myPhone==null || HomePresrenter.myPhone.isEmpty()){
//            if(Register.phonenumber==null || Register.phonenumber.isEmpty()){
//                phonenumber = EnterPage.enterphonenumber;
//            }else{
//                phonenumber =Register.phonenumber;
//            }
//        }else {
//            phonenumber =HomePresrenter.myPhone;
//        }
//        DBHelper dbHelper = DBHelper.getInstance(setMiyaoAgain);
//        SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
//        Cursor cursor=sqLiteDatabase.query("tipacount", new String[]{"tip"}, "phone= '"+phonenumber+"'", null,null, null, null);
//        while (cursor.moveToNext()){
//            value =cursor.getString(0);
//        }
//        cursor.close();
//        return value;
//    }

    public void verifyKey(String token,String key){
        Call<ResponseInfo> verifyKeyInfo = responseInfoApi.verifykey(token,key);
        verifyKeyInfo.enqueue(new CallBackAdapter());
    }

    public void setKetTip(String token,String keyTip){
        Call<ResponseInfo> seyKeyTipInfo = responseInfoApi.setkeytip(token,keyTip);
        seyKeyTipInfo.enqueue(new CallBackAdapter());
    }

    public void encrypt(String token,String accountName,String account,String password){
        Call<ResponseInfo> acountAdd = responseInfoApi.acountadd(token, accountName, account, password);
        acountAdd.enqueue(new CallBackAdapter());
    }
}
